<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin: 10px 0">
      <div v-show="scene == 0">
        <el-button
          type="primary"
          icon="Plus"
          :disabled="categoryStore.c3Id ? false : true"
          @click="addAttr"
        >
          添加品牌属性
        </el-button>
        <el-table style="margin: 10px 0" border :data="attrList">
          <el-table-column
            align="center"
            type="index"
            label="序号"
            width="80px"
          />
          <el-table-column prop="attrName" label="属性名称" width="120px" />
          <el-table-column prop="address" label="属性值名称">
            <template #="{ row }">
              <el-tag
                style="margin: 5px"
                type="primary"
                v-for="item in row.attrValueList"
                :key="item.id"
              >
                {{ item.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #="{ row }">
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                @click="updateAttr(row)"
              />
              <el-popconfirm
                :title="`你确定要删除${row.attrName}?`"
                width="200px"
                @confirm="deleteAttr(row.id)"
              >
                <template #reference>
                  <el-button type="primary" size="small" icon="Delete" />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="scene == 1">
        <el-form :inline="true">
          <el-form-item label="属性名称" width="200px">
            <el-input
              placeholder="请输入属性的名称"
              v-model="attrParams.attrName"
            />
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          icon="Plus"
          :disabled="attrParams.attrName ? false : true"
          @click="addAttrValue"
        >
          添加属性值
        </el-button>
        <el-button @click="cancel">取消</el-button>
        <el-table
          border
          style="margin: 10px 0"
          :data="attrParams.attrValueList"
        >
          <el-table-column
            label="序号"
            width="120px"
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column label="属性值">
            <template #="{ row, $index }">
              <el-input
                placeholder="请输入属性值名称"
                v-model="row.valueName"
                size="small"
                v-if="row.flag"
                @blur="toLook(row, $index)"
                :ref="(vc: any) => (inputArr[$index] = vc)"
              />
              <div v-else @click="toEdit(row, $index)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #="{ $index }">
              <el-button
                icon="Delete"
                size="small"
                type="danger"
                @click="deleteAttrValue($index)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          @click="saveAttr"
          :disabled="attrParams.attrValueList.length > 0 ? false : true"
        >
          保存
        </el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, reactive, nextTick, onBeforeUnmount } from 'vue'
import { reqAttr, reqAddOrUpdateAttr, reqDeleteAttr } from '@/api/product/attr'
import type {
  ResponseAttrData,
  AttrList,
  Attr,
  AttrValue,
} from '@/api/product/attr/type'
import { ElMessage } from 'element-plus'
import useCategoryStore from '@/store/modules/category'
let categoryStore = useCategoryStore()
let attrList = ref<AttrList>([])
let scene = ref<number>(0)
let attrParams = reactive<Attr>({
  attrName: '',
  attrValueList: [],
  categoryId: '',
  categoryLevel: 3,
})
watch(
  () => categoryStore.c3Id,
  () => {
    attrList.value = []
    if (!categoryStore.c3Id) return
    getAttr()
  },
)
const getAttr = () => {
  const { c1Id, c2Id, c3Id } = categoryStore
  reqAttr(c1Id, c2Id, c3Id).then((res: ResponseAttrData) => {
    if (res.code == 200) {
      attrList.value = res.data
    }
  })
}
const addAttrValue = () => {
  attrParams.attrValueList.push({
    valueName: '',
    flag: true,
  })
  nextTick(() => {
    inputArr.value[attrParams.attrValueList.length - 1].focus()
  })
}
const addAttr = () => {
  Object.assign(attrParams, {
    attrName: '',
    attrValueList: [],
    categoryId: categoryStore.c3Id,
    categoryLevel: 3,
  })
  scene.value = 1
}
const updateAttr = (row: AttrValue) => {
  scene.value = 1
  Object.assign(attrParams, JSON.parse(JSON.stringify(row)))
}
const cancel = () => {
  scene.value = 0
}
const saveAttr = () => {
  reqAddOrUpdateAttr(attrParams).then((res: any) => {
    if (res.code == 200) {
      scene.value = 0
      ElMessage({
        type: 'success',
        message: attrParams.id ? '修改属性值成功' : '添加属性值成功',
      })
      getAttr()
    } else {
      ElMessage({
        type: 'error',
        message: attrParams.id ? '修改属性值失败' : '添加属性值失败',
      })
    }
  })
}
const toLook = (row: AttrValue, index: number) => {
  if (row.valueName.trim() == '') {
    attrParams.attrValueList.splice(index, 1)
    return ElMessage({
      type: 'error',
      message: '属性值不能为空！',
    })
  }
  let repeat = attrParams.attrValueList.find((a, i) => {
    if (i != index) {
      return a.valueName == row.valueName
    }
  })
  if (repeat) {
    attrParams.attrValueList.splice(index, 1)
    return ElMessage({
      type: 'error',
      message: '属性值不能重复！',
    })
  }
  row.flag = false
}
const toEdit = (row: AttrValue, $index: number) => {
  row.flag = true
  nextTick(() => {
    inputArr.value[$index].focus()
  })
}
let inputArr = ref<any>([])
const deleteAttrValue = ($index: number) => {
  attrParams.attrValueList.splice($index, 1)
}
const deleteAttr = (attrId: number) => {
  reqDeleteAttr(attrId).then((res: any) => {
    if (res.code == 200) {
      ElMessage({
        type: 'success',
        message: '删除属性成功！',
      })
      getAttr()
    } else {
      ElMessage({
        type: 'error',
        message: '删除属性失败！',
      })
    }
  })
}
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>
<script lang="ts">
export default {
  name: 'Attr',
}
</script>

<style lang="scss"></style>
